package com.fy.fyspace.controller;

import com.fy.fyspace.common.annotation.GlobalInterceptor;
import com.fy.fyspace.common.result.Result;
import com.fy.fyspace.model.dto.user.UserQueryDTO;
import com.fy.fyspace.model.entity.User;
import com.fy.fyspace.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "管理员模块接口", description = "提供管理员相关的接口操作")
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户列表", notes = "根据查询条件获取系统用户列表信息，支持分页查询和条件筛选，仅管理员可访问")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求失败")
    })
    @GlobalInterceptor(checkAdmin = true)
    @PostMapping("/userList")
    public Result list(@ApiParam(value = "用户查询参数") @RequestBody UserQueryDTO userQueryDTO) {
        log.info("查询用户列表/userQueryDTO:{}", userQueryDTO);
        return Result.success(userService.getUserList(userQueryDTO));
    }

    /**
     * 禁用用户
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "禁用用户", notes = "禁用用户，仅管理员可访问")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求失败")
    })
    @GlobalInterceptor(checkAdmin = true)
    @PostMapping("/forbid/{id}/{status}")
    public Result disableUser(@ApiParam(value = "用户id") @PathVariable Long id, @ApiParam(value = "用户状态") @PathVariable Integer status) {
        log.info("禁用用户/id:{}", id);
        log.info("禁用用户/status:{}", status);
        User user = userService.getById(id);
        user.setIsForbid(status);
        boolean b = userService.updateById(user);
        return Result.success(b);
    }

    /**
     * 导出用户信息表
     * @param isForbid
     * @param month
     * @param response
     */
    @ApiOperation(value = "导出用户信息表", notes = "导出指定月份、指定状态的员工信息表")
    @GlobalInterceptor(checkAdmin = true)
    @GetMapping("/export")
    public void exportUserExcel(
            @RequestParam(value = "isForbid", required = false) String isForbidStr,
            @RequestParam(value = "month", required = false) String month,
            HttpServletResponse response) {
        // 兼容前端传"null"字符串的情况
        if (month != null && "null".equalsIgnoreCase(month.trim())) {
            month = null;
        }
        Integer isForbid = null;
        if (isForbidStr != null && !"null".equalsIgnoreCase(isForbidStr.trim()) && !isForbidStr.trim().isEmpty()) {
            try {
                isForbid = Integer.valueOf(isForbidStr.trim());
            } catch (NumberFormatException e) {
                // 记录日志，忽略非法isForbid参数
                log.warn("isForbid参数非法: {}", isForbidStr);
            }
        }
        log.info("导出用户信息表/isForbid:{}", isForbid);
        log.info("导出用户信息表/month:{}", month);
        userService.exportUserExcel(isForbid, month, response);
    }
}
